import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/login.vue'
import {store} from '../js/'


const Router = createRouter({
  history: createWebHashHistory(),
  routes: [{
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('../views/AdminLayout.vue'),
    children: [
        {
            path: "",
            name: 'welcome',
            component: () => import('../views/Welcome.vue'),
            meta: { title: '首页' }
        },
        {
          path: "/users",
          name: 'users',
          component: () => import('../components/user/user.vue'),
          meta: { title: '用户' }
        }
        ]
  }
  
  ]
})

Router.beforeEach((to, from, next) => {
  if (to.path === "/login") {
      store.remove('token');
  }
  // console.log('to.path', to.path);
  var token = store.get("token");
  if (!token && to.path != "/login") {
      next({
          path: "/login"
      });
  } else {

      let bcs = [];//面包屑
      if (to.meta && to.meta.bcs) {
          bcs = bcs.concat(to.meta.bcs);
      }
      if (to.meta && to.meta.title) {
          bcs.push(to.meta.title);
          document.title = to.meta.title + ' - ' + '后台管理';
      } else {
          document.title = "后台管理";
      }
      store.resetBcs(bcs);
      next();
  }
});


export default Router